package com.zhx.threadcoreknowledge.synchromized;

/**
 * @author ：zhanghaixuan
 * @date ：Created in 2019-10-01 21:09
 * 用途         ： 对象示例1,代码块形式
 */
public class SunchronizedObjectCodeBlock2 implements Runnable{
    static SunchronizedObjectCodeBlock2 instance = new SunchronizedObjectCodeBlock2();

    @Override
    public void run() {
        synchronized (this){
            System.out.println("对象锁代码块形式: " + Thread.currentThread().getName());

            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println("运行结束: " + Thread.currentThread().getName());
        }
    }

    public static void main(String[] args) {
        Thread thread1 = new Thread(instance);
        Thread thread2 = new Thread(instance);
        thread1.start();
        thread2.start();

        while (thread1.isAlive() || thread2.isAlive()){

        }

        System.out.println("finashed");
    }
}
